<template>
  <a-modal v-model="visible" :mask-closable="false" width="75%" title="查看" :dialog-style="{ top: '20px' }">
    <div v-if="visible" v-permission="['retail:return:query']" v-loading="loading">
      <j-border>
        <j-form>
          <j-form-item label="仓库">
            {{ formData.scName }}
          </j-form-item>
          <j-form-item label="会员">
            {{ formData.memberName }}
          </j-form-item>
          <j-form-item label="销售员">
            {{ formData.salerName }}
          </j-form-item>
          <j-form-item label="付款日期">
            {{ formData.paymentDate }}
          </j-form-item>
          <j-form-item label="零售出库单" :span="16">
            <div v-if="!$utils.isEmpty(formData.outSheetCode)">
              <a v-permission="['retail:out:query']" @click="e => $refs.viewOutSheetDetailDialog.openDialog()">{{ formData.outSheetCode }}</a>
              <span v-no-permission="['retail:out:query']">{{ formData.outSheetCode }}</span>
            </div>
          </j-form-item>
          <j-form-item label="状态">
            <span v-if="$enums.RETAIL_RETURN_STATUS.APPROVE_PASS.equalsCode(formData.status)" style="color: #52C41A;">{{ $enums.RETAIL_RETURN_STATUS.getDesc(formData.status) }}</span>
            <span v-else-if="$enums.RETAIL_RETURN_STATUS.APPROVE_REFUSE.equalsCode(formData.status)" style="color: #F5222D;">{{ $enums.RETAIL_RETURN_STATUS.getDesc(formData.status) }}</span>
            <span v-else style="color: #303133;">{{ $enums.RETAIL_RETURN_STATUS.getDesc(formData.status) }}</span>
          </j-form-item>
          <j-form-item label="拒绝理由" :span="16" :content-nest="false">
            <a-input v-if="$enums.RETAIL_RETURN_STATUS.APPROVE_REFUSE.equalsCode(formData.status)" v-model="formData.refuseReason" read-only />
          </j-form-item>
          <j-form-item label="操作人">
            <span>{{ formData.createBy }}</span>
          </j-form-item>
          <j-form-item label="操作时间" :span="16">
            <span>{{ formData.createTime }}</span>
          </j-form-item>
          <j-form-item v-if="$enums.RETAIL_RETURN_STATUS.APPROVE_PASS.equalsCode(formData.status) || $enums.RETAIL_RETURN_STATUS.APPROVE_REFUSE.equalsCode(formData.status)" label="审核人">
            <span>{{ formData.approveBy }}</span>
          </j-form-item>
          <j-form-item v-if="$enums.RETAIL_RETURN_STATUS.APPROVE_PASS.equalsCode(formData.status) || $enums.RETAIL_RETURN_STATUS.APPROVE_REFUSE.equalsCode(formData.status)" label="审核时间" :span="16">
            <span>{{ formData.approveTime }}</span>
          </j-form-item>
        </j-form>
      </j-border>
      <!-- 数据列表 -->
      <vxe-grid
        ref="grid"
        resizable
        show-overflow
        highlight-hover-row
        keep-source
        row-id="id"
        height="500"
        :data="tableData"
        :columns="tableColumn"
      >
        <!-- 是否赠品 列自定义内容 -->
        <template v-slot:isGift_default="{ row }">
          <span>{{ row.isGift ? '是' : '否' }}</span>
        </template>

        <!-- 含税金额 列自定义内容 -->
        <template v-slot:taxAmount_default="{ row }">
          <span v-if="$utils.isFloatGeZero(row.taxPrice) && $utils.isIntegerGeZero(row.returnNum)">{{ $utils.mul(row.taxPrice, row.returnNum) }}</span>
        </template>
      </vxe-grid>

      <order-time-line :id="id" />

      <j-border title="合计">
        <j-form label-width="140px">
          <j-form-item label="退货数量" :span="6">
            <a-input v-model="formData.totalNum" class="number-input" read-only />
          </j-form-item>
          <j-form-item label="赠品数量" :span="6">
            <a-input v-model="formData.giftNum" class="number-input" read-only />
          </j-form-item>
          <j-form-item label="含税总金额" :span="6">
            <a-input v-model="formData.totalAmount" class="number-input" read-only />
          </j-form-item>
        </j-form>
      </j-border>

      <j-border title="支付方式">
        <pay-type ref="payType" :disabled="true" />
      </j-border>

      <j-border>
        <j-form label-width="140px">
          <j-form-item label="备注" :span="24" :content-nest="false">
            <a-textarea v-model.trim="formData.description" maxlength="200" read-only />
          </j-form-item>
        </j-form>
      </j-border>
    </div>
    <template slot="footer">
      <div class="form-modal-footer">
        <a-space>
          <a-button type="primary" :loading="loading" @click="print">打印</a-button>
          <a-button :loading="loading" @click="closeDialog">关闭</a-button>
        </a-space>
      </div>
    </template>
    <!-- 零售出库单查看窗口 -->
    <out-sheet-detail :id="formData.outSheetId" ref="viewOutSheetDetailDialog" />
  </a-modal>
</template>
<script>
import OutSheetDetail from '@/views/sc/retail/out/detail'
import { getLodop } from '@/utils/lodop'
import PayType from '@/views/sc/pay-type/index'

export default {
  components: {
    OutSheetDetail, PayType
  },
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 是否可见
      visible: false,
      // 是否显示加载框
      loading: false,
      // 表单数据
      formData: {},
      // 列表数据配置
      tableColumn: [
        { type: 'seq', width: 40 },
        { field: 'productCode', title: '商品编号', width: 120 },
        { field: 'productName', title: '商品名称', width: 260 },
        { field: 'skuCode', title: '商品SKU编号', width: 120 },
        { field: 'externalCode', title: '商品外部编号', width: 120 },
        { field: 'unit', title: '单位', width: 80 },
        { field: 'spec', title: '规格', width: 80 },
        { field: 'categoryName', title: '商品类目', width: 120 },
        { field: 'brandName', title: '商品品牌', width: 120 },
        { field: 'retailPrice', title: '参考零售价（元）', align: 'right', width: 150 },
        { field: 'isGift', title: '是否赠品', width: 80, slots: { default: 'isGift_default' }},
        { field: 'discountRate', title: '折扣（%）', align: 'right', width: 120 },
        { field: 'taxPrice', title: '价格（元）', align: 'right', width: 120 },
        { field: 'outNum', title: '出库数量', align: 'right', width: 100, formatter: ({ cellValue }) => { return this.$utils.isEmpty(cellValue) ? '-' : cellValue } },
        { field: 'remainNum', title: '剩余退货数量', align: 'right', width: 120, formatter: ({ cellValue }) => { return this.$utils.isEmpty(cellValue) ? '-' : cellValue } },
        { field: 'returnNum', title: '退货数量', align: 'right', width: 100 },
        { field: 'taxAmount', title: '含税金额', align: 'right', width: 120, slots: { default: 'taxAmount_default' }},
        { field: 'taxRate', title: '税率（%）', align: 'right', width: 100 },
        { field: 'description', title: '备注', width: 200 }
      ],
      tableData: []
    }
  },
  computed: {
  },
  created() {
    // 初始化表单数据
    this.initFormData()
  },
  methods: {
    // 打开对话框 由父页面触发
    openDialog() {
      this.visible = true

      this.$nextTick(() => this.open())
    },
    // 关闭对话框
    closeDialog() {
      this.visible = false
      this.$emit('close')
    },
    // 初始化表单数据
    initFormData() {
      this.formData = {
        scName: '',
        memberName: '',
        salerName: '',
        paymentDate: '',
        outSheetId: '',
        outSheetCode: '',
        totalNum: 0,
        giftNum: 0,
        totalAmount: 0,
        description: ''
      }

      this.tableData = []
    },
    // 加载数据
    loadData() {
      this.loading = true
      this.$api.sc.retail.retailReturn.get(this.id).then(res => {
        this.formData = {
          scName: res.scName,
          memberName: res.memberName,
          salerName: res.salerName || '',
          paymentDate: res.paymentDate || '',
          outSheetId: res.outSheetId || '',
          outSheetCode: res.outSheetCode || '',
          description: res.description,
          status: res.status,
          createBy: res.createBy,
          createTime: res.createTime,
          approveBy: res.approveBy,
          approveTime: res.approveTime,
          refuseReason: res.refuseReason,
          totalNum: 0,
          giftNum: 0,
          totalAmount: 0
        }
        this.tableData = res.details || []

        this.$refs.payType.setTableData(res.payTypes || [])
        this.calcSum()
      }).finally(() => {
        this.loading = false
      })
    },
    // 页面显示时触发
    open() {
      // 初始化表单数据
      this.initFormData()

      this.loadData()
    },
    // 计算汇总数据
    calcSum() {
      let totalNum = 0
      let giftNum = 0
      let totalAmount = 0

      this.tableData.filter(t => {
        return this.$utils.isFloatGeZero(t.taxPrice) && this.$utils.isIntegerGeZero(t.returnNum)
      }).forEach(t => {
        const num = parseInt(t.returnNum)
        if (t.isGift) {
          giftNum = this.$utils.add(giftNum, num)
        } else {
          totalNum = this.$utils.add(totalNum, num)
        }

        totalAmount = this.$utils.add(totalAmount, this.$utils.mul(num, t.taxPrice))
      })

      this.formData.totalNum = totalNum
      this.formData.giftNum = giftNum
      this.formData.totalAmount = totalAmount
    },
    print() {
      this.loading = true
      this.$api.sc.retail.retailReturn.print(this.id).then(res => {
        const LODOP = getLodop(res, '打印零售退货单')
        LODOP.PREVIEW()
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>
<style>
</style>
